<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\InsideChurujiluExport;
use App\Exports\InsideXiehuoExport;
use App\Exports\InsideZhuanghuoExport;

class SummaryController extends Controller
{
	use ModelOperatorTrait;


	protected function model(){
        return app(\App\Models\Summary::class);
    }

    public function index(Request $request){

    	$summaries = $this->queryModel($request->all());

    	if(isset($request->start_time)){
            if(isset($request->end_time)){
                $summaries = $summaries->where('created_at','>=',$request->start_time)->where('created_at','<=',$request->end_time);
            }
        } 

         if(request()->has('page')){
            $pageSize = 10;
            if(request()->has('pageSize')){
                $pageSize = request()->pageSize;
            }
            $summaries = $summaries->orderBy('created_at','desc')->paginate($pageSize);
        }
        else{
            $summaries = $summaries->get();
        }

    	return formatJsonResponse($summaries);
    }

    public function export(Request $request){

    	if(isset($request->empty_type)){
    		switch ($request->empty_type) {
    			case '0':
    				return Excel::download(new InsideZhuanghuoExport($request->all()), '内部流程汇总表（装货）'. time() .'.xlsx');
    				break;
    			case '1':
    				return Excel::download(new InsideXiehuoExport($request->all()), '内部流程汇总表（卸货）'. time() .'.xlsx');
    				break;
    		}
    	}
    	else{
    		return Excel::download(new InsideChurujiluExport($request->all()), '内部流程汇总表（出入记录）'. time() .'.xlsx');
    	}


    }
}
